﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace ClipboardTranslator
{
    public partial class EditableComboBox : UserControl
    {
        public delegate void ItemActionHandler();

        public event ItemActionHandler OnNewItem;

        public event ItemActionHandler OnRemoveItem;

        public event ItemActionHandler OnSelectItem;

        public event ItemActionHandler OnSaveItem;

        public void Reset(string[] items)
        {
            comboBox.Items.Clear();
            comboBox.Items.AddRange(items);
            comboBox.AutoCompleteCustomSource.AddRange(items);
        }

        public void Select(string item)
        {
            int index = comboBox.Items.IndexOf(item);
            if (index >= 0)
                comboBox.SelectedIndex = index;
            else 
                comboBox.SelectedIndex = 0;
        }

        public new string Text { get { return comboBox.Text; } }

        public EditableComboBox()
        {
            InitializeComponent();
            comboBox.LostFocus += new EventHandler(comboBox_LostFocus);
            menuItemSave.Text = Localization.Localize(menuItemSave.Text);
            menuItemRemove.Text = Localization.Localize(menuItemRemove.Text);
        }

        void DoNewItem()
        {
            if (!string.IsNullOrEmpty(comboBox.Text) && !comboBox.Items.Contains(comboBox.Text))
            {
                if (OnNewItem != null)
                    OnNewItem();

                comboBox.Items.Add(comboBox.Text);

                if (OnSelectItem != null)
                    OnSelectItem();
            }
        }

        void comboBox_LostFocus(object sender, EventArgs e)
        {
            DoNewItem();
        }

        private void EditableComboBox_SizeChanged(object sender, EventArgs e)
        {
            Height = comboBox.Height + Utils.GetBorderSize(BorderStyle).Height;
        }

        private void menuItemRemove_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(comboBox.Text) && comboBox.Items.Contains(comboBox.Text))
            {
                if (OnRemoveItem != null)
                    OnRemoveItem();
                comboBox.Items.Remove(comboBox.Text);
            }
        }

        private void comboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (OnSelectItem != null)
                OnSelectItem();
        }

        private void comboBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (Keys.Enter == e.KeyCode) DoNewItem();
            else if (Keys.Escape == e.KeyCode) comboBox.Text = "";
        }

        private void menuItemSave_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(comboBox.Text) && comboBox.Items.Contains(comboBox.Text))
            {
                if (OnSaveItem != null)
                    OnSaveItem();
            }
        }
    }
}
